package com.hjm.mall.config;

import com.fasterxml.classmate.TypeResolver;
import com.hjm.mall.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.AlternateTypeRules;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hjm.mall.api"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(this.getParameterList())
                .alternateTypeRules(AlternateTypeRules.newMapRule(String.class, List.class))
                .alternateTypeRules(AlternateTypeRules.newMapRule(List.class, Integer.class));
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("哈基米移动商城 接口文档")
                .description("文档编辑 by 你爸爸")
                .version("1.0")
                .build();
    }

    /**
     * 添加head参数配置
     */
    private List<Parameter> getParameterList() {
        ParameterBuilder clientIdTicket = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        clientIdTicket.name("token").description("token令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build(); //设置false，表示clientId参数 非必填,可传可不传！
        pars.add(clientIdTicket.build());
        return pars;
    }

}
